//jQuery(document).ready(function(){
//	// 绑定jquery的验证插件
//	jQuery("#plantform").validationEngine({
//		ajaxFormValidation: true,
//		validationEventTriggers:"blur", 
//		onAjaxFormComplete: ajaxValidationCallback
//	});
//	alert("ff");
//});
//function ajaxValidationCallback(status, form, json, options){
//    alert(status + ",form:" + form +",json:" + json + ",option:" + options ); 
//	if (status == true) {
//		alert("the form is valid!");
//		// uncomment these lines to submit the form to form.action
//		// form.validationEngine('detach');
//		// form.submit();
//		// or you may use AJAX again to submit the data
//	} else{
//		alert("else");
//	}
//}
var filecount = 1;
var locationCount = 1;
var markerCount = 0;
var markers = new Array();
var map;
var xhr;//通过ajax得到某个分科科下所有属的变量
var xhr_kindke;
var xhr_kindshu;
var ke;
var shu;
/**
 * 修改信息界面
 */
var addOrEdit = 1;
var init_kindke;//修改界面时，科的初始值
var init_kindshu;
var init_subject;
//存储植物地理位置，得到初始化地图后再标志出这些地理位置
var longitudes = new Array();
var latitudes = new Array();
var count = 0;//地理位置的数量

/**
 * 提交表单前，先验证下输入内容的合法性
 * @return
 */
function validation()
{
	return true;
}
/**
 * 增加分类
 * @param kind
 * @return
 */
function addkind(kind)
{
	if(kind == 1)//增加科分类
	{
		var kind = window.prompt('请输入新的科分类：');
		ke = kind;
		var json_url = "json/plantManager-addKindKe.html?kindKe="+kind;
		xhr_kindke = null;
		xhr_kindke = connectXHR(json_url, addKindKe);
	}
	else//增加属分类
	{
		var kindKe = document.getElementById("ke").value;
		if(kindKe==null || kindKe==""){
			alert("请先选择科");
			return;
		}
		var kind = window.prompt('请输入新的属分类：');
		shu = kind;
		var json_url = "json/plantManager-addKindShu.html?kindKe="+kindKe+"&kindshu="+kind;
		xhr_kindshu = null;
		xhr_kindshu = connectXHR(json_url, addKindShu);
	}
}

function addKindKe(){
	if(xhr_kindke == null)return;
	if(xhr_kindke.readyState == 4 && xhr_kindke.status == 200) {
		var data = eval('(' + xhr_kindke.responseText + ')');
		var isExit = data.isKindKeExit;
		var idkind = data.kindKe;
		var kelist = document.getElementById("ke");
		if(isExit == "false"){
			alert("系统中已经存在此科分类");
		}else{
			var ops = document.createElement("option");
			ops.setAttribute("value", idkind);
			ops.appendChild(document.createTextNode(ke));
			kelist.appendChild(ops);
			kelist.value=idkind;
		}
	}
}
function addKindShu(){
	if(xhr_kindshu == null)return;
	if(xhr_kindshu.readyState == 4 && xhr_kindshu.status == 200) {
		var data = eval('(' + xhr_kindshu.responseText + ')');
		var isExit = data.isKindShuExit;
		var idkind = data.kindshu;
		var shulist = document.getElementById("shu");
		if(isExit == "false"){
			alert("系统中已经存在此科分属");
		}else{
			var ops = document.createElement("option");
			ops.setAttribute("value", idkind);
			ops.appendChild(document.createTextNode(shu));
			shulist.appendChild(ops);
			shulist.value=idkind;
		}
	}
}
/**
 * 增加一个上传图片的上传控件
 * 
 * @return
 */
function addphoto()
{
	filecount++;
	//var photohtml = '<br/><br/><div class="text"></div><input type="file" value="上传" size="20" name="upload"/>';
	var photohtml = '<br/><br/><div class="text"></div><input type="file" class="file" accept="image/*" id="f'
				+ filecount + '" name="upload" onchange="t' + filecount
				+ '.value=this.value"/><input type="text" id="t' + filecount
				+ '" readonly="readonly"></input><input type="button" value="上传" class="addkind"'
				+ ' onclick="f' + filecount + '.click()"/>';
	var html = document.createElement("div");
	html.innerHTML = photohtml;
	
	document.getElementById("photos").appendChild(html);
}
/**
 * 删除一个上传图片的上传控件
 * @return
 */
function removephoto()
{
	var photo = document.getElementById("photos");
	var length = 0;
	for(var i = 0; i<photo.childNodes.length;i++)
	{
		if(photo.childNodes[i].nodeType == 1)
			length++;
	}
	if(length == 1)
		return;
	photo.removeChild(getLastchild(photo));
}
/**
 * 得到一个结点的最后一个子结点
 * @param photo
 * @return
 */
function getLastchild(photo)
{
	var node = photo.lastChild;
	while(node.nodeType != 1)
	{
		node = node.previousSibling;
	}
	return node;
}
/**
 * 得到分类科下的所有分类属
 * @param kindshu
 * @return
 */
function getKindKe(kindke)
{
	if(kindke == null || kindke == "")
	{
		alert("empty");
		kindke = 0;
	}
	var kindShulist = document.getElementById("shu");
	 //把原来的分类科下拉框删掉
	for (var i = kindShulist.childNodes.length; i > 2; i--){
		kindShulist.removeChild(kindShulist.lastChild);
	}
	
	var json_url = "json/plantManager-listKindShuByKindKe.html?kindKe="+kindke;
	xhr = null;
	xhr = connectXHR(json_url, listKindShu);
}
/**
 * 填充分类属选择框
 * @return
 */
function listKindShu()
{
	if(xhr == null)return;
	if(xhr.readyState == 4 && xhr.status == 200) {
		var data = eval('(' + xhr.responseText + ')');
		var kindshu = data;
		if(kindshu == null || kindshu.length == 0)
			return;
		var kindShulist = document.getElementById("shu");
		for(var i = 0; i < kindshu.length; i++)
		{
			var ops = document.createElement("option");
			ops.setAttribute("value", kindshu[i].value);
			ops.appendChild(document.createTextNode(kindshu[i].kindShuName));
			kindShulist.appendChild(ops);
		}
	}
}
function initMap()
{
	var latlng = new google.maps.LatLng(24.437736364171066,118.09754759073257);
	map = new google.maps.Map(document.getElementById('map'), {  
	    zoom: 14,  
	    center: latlng,  
	    mapTypeId: google.maps.MapTypeId.ROADMAP  
	});
	google.maps.event.addListener(map, 'click', function(event) {
	    placeMarker(event.latLng);
	});
	var sw =  new google.maps.LatLng(24.429128461693962,118.07772142648697);
	var ne =  new google.maps.LatLng(24.45523099834168,118.1123082947731);
	//自定义地图的显示范围:厦大周边  
	var strictBounds = new google.maps.LatLngBounds(  
	    sw, ne);  
	google.maps.event.addListener(map, 'dragend',  
	function() {  
	    if (strictBounds.contains(map.getCenter())) return;  
	    var c = map.getCenter(),  
	    x = c.lng(),  
	    y = c.lat(),  
	    maxX = strictBounds.getNorthEast().lng(),  
	    maxY = strictBounds.getNorthEast().lat(),  
	    minX = strictBounds.getSouthWest().lng(),  
	    minY = strictBounds.getSouthWest().lat();  
	    if (x < minX) x = minX;  
	    if (x > maxX) x = maxX;  
	    if (y < minY) y = minY;  
	    if (y > maxY) y = maxY;  
	    map.setCenter(new google.maps.LatLng(y, x));  
	});
	//在地图上标记处植物地理位置
	for(var i=0; i<count; i++)
	{
		var lng = Number(longitudes[i]);
		var lat = Number(latitudes[i]);
		var location = new google.maps.LatLng(lat,lng);
		placeMarker(location);
		addlocation();
	}
}
/**
 * 放置一个图标在地图上
 * @param location
 * @return
 */
function placeMarker(location) {

	if(markers[markerCount] != null){
		markers[markerCount].setMap(null);
	}

	var marker = new google.maps.Marker( {
		position : location,
		map : map
	});
	markers[markerCount] = marker;

	map.setCenter(location);
	var lngtext = document.getElementById("lng" + locationCount);
	lngtext.value = location.lng();
	var lattext = document.getElementById("lat" + locationCount);
	lattext.value = location.lat();
}
/**
 * 增加一个地理位置的经纬度输入框
 * 
 * @return
 */
function addlocation() {
	locationCount++;
	var photohtml = '<br/><br/><div class="text"><label>经度</label></div><input type="text" class="longitude" id="lng'
			+ locationCount
			+ '" name="plant.longitude" readonly="readonly"/>'
			+ '<br /><br /><div class="text"><label>纬度</label></div><input type="text" class="latitude" id="lat'
			+ locationCount
			+ '" name="plant.latitude" readonly="readonly"/>';
	var html = document.createElement("div");
	html.innerHTML = photohtml;

	document.getElementById("location").appendChild(html);
	
	markerCount++;
}
/**
 * 删除最后一个经纬度输入框
 * @return
 */
function removelocation(){
	var photo = document.getElementById("location");
	var length = 0;
	for(var i = 0; i<photo.childNodes.length;i++)
	{
		if(photo.childNodes[i].nodeType == 1)
			length++;
	}
	if(length == 1)
		return;
	photo.removeChild(getLastchild(photo));
	locationCount--;
	if(markers[markerCount] != null){
		markers[markerCount].setMap(null);
	}
	markerCount--;
}
/**
 * 修改界面时，科的初始化
 * @return
 */
function initKindke(kindke){
	var kelist = document.getElementById("ke");
	kelist.value = kindke;
}
/**
 * 修改界面时，属的初始化
 * @return
 */
function initKindshu(kindshu){
	var shulist = document.getElementById("shu");
	shulist.value = kindshu;
}
/**
 * 修改界面时，专题的初始化
 * @return
 */
function initSubject(sub){
	var subjectlist = document.getElementById("subject");
	subjectlist.value = sub;
}

/**
 * 先存储地理位置
 * @param longitude
 * @param latitude
 * @return
 */
function storeLocation(longitude, latitude){
	longitudes[count] = longitude;
	latitudes[count] = latitude;
	count++;
}
/**
 * 删除植物已有照片
 * @param index
 * @return
 */
function deleteImage(index){
	var image = document.getElementById("pimages");
	var imgdiv = document.getElementById("imgdiv" + index);
	var imgbutton = document.getElementById("imgbutton" + index);
	var imghidden = document.getElementById("pimg" + index);
	image.removeChild(imgdiv);
	image.removeChild(imgbutton);
	image.removeChild(imghidden);
}
/**
 * 删除植物已有地理位置
 * @param index
 * @return
 */
function deleteLocationNode(index){
	
}